using System;
using System.Collections.Generic;

namespace Test.ConsoleProgram.Algorithm.Solution
{
    [TestDescription("算法: 0459. 重复的子字符串")]
    public class No0459_RepeatedSubstringPattern : AbsBaseTestItem
    {
        public override void OnTest()
        {
            Assert.TestExe(RepeatedSubstringPattern, "abab", true);
            Assert.TestExe(RepeatedSubstringPattern, "aba", false);
            Assert.TestExe(RepeatedSubstringPattern, "abcabcabcabc", true);
        }

        public bool RepeatedSubstringPattern(string s)
        {
            int n = s.Length;
            for (int i = 1; i * 2 <= n; ++i)
            {
                if (n % i == 0)
                {
                    bool match = true;
                    for (int j = i; j < n; ++j)
                    {
                        if (s[j] != s[j - i])
                        {
                            match = false;
                            break;
                        }
                    }
                    if (match)
                    {
                        return true;
                    }
                }
            }
            return false;
        }
    }
}
